<?php
namespace App\Models;
use stdClass;
class RoleModel extends BaseModel {
	public function __construct() {
		parent::__construct();
		$this -> _table = "Role";
	}
	public function Roleselect($rolename){
		$sql=
		"SELECT
		USER .ID,	USER .UserName,	USER .Email
	    FROM role
		INNER JOIN `user` ON `user`.RoleID = role.ID
		WHERE  RoleName = '" . $rolename . "'";
		
		$sth = $this -> _dbHandle -> prepare($sql);
		$sth -> execute();
		return $result= $sth -> fetchAll(); 
	}
	
	public function RoleItem(){
		$sql = sprintf("select * from `%s` %s", $this -> _table, $this -> filter);
		$time='order by time DESC';
		//        排序
		$order_column = $_GET['order']['0']['column']; // 哪一列排序
		$order_dir= $_GET['order']['0']['dir']; // ase desc 升序或者降序
		//拼接排序sql
		$orderSql = "";
		if($order_column>=1){
		    $i = intval($order_column);
		  switch($i){
		     case 1;$orderSql = " order by id ".$order_dir;break;
		     default;$orderSql = '';
		  }
		   $sth = $this -> _dbHandle -> prepare($sql.$orderSql);
		}else{
			$sth = $this -> _dbHandle -> prepare($sql.$time);
			  
		}  
		$sth -> execute();
		$result= $sth -> fetchAll(); 
		
		//		分页
		$draw = $_GET["draw"];
		$start = $_GET["start"];
		$length = $_GET["length"];
		$newarr = new stdClass();
		$newarr->draw = $draw;
		$newarr->page = count($result)/$length;
		$newarr->recordsTotal = count($result);
		$newarr->recordsFiltered = count($result);
		$newarr->data = array();
		$h=0;
		for($i = $start; $i<$start+$length;$i++,$h++)
			{   if(!empty($result[$i])){
				$newarr->data[$h] = $result[$i];
				}else{
					break;
				}
						
			}
				
			echo json_encode($newarr);
		}
	/**/
}
